package model;

import java.util.Set;

import newspapers.autopublishing.AutoNewspaper;
import newspapers.autopublishing.TimeSeg;

import users.Publisher;
import users.UpdateMessageListener;

/**
 * Publisher to a Qpid broker.
 */
public class Newspaper extends Publisher {

    /** Name of the newspaper. */
    private String name;
    /**
     * Getter for the variable 'name'.
     * @return variable 'name'
     */
    public String getName() {
        return name;
    }
    /**
     * Setter for the variable 'name'.
     * @param name  new value of the variable 'name'
     */
    public void setName(final String name) {
        this.name = name;
    }

    /** Prefix of the topics this newspaper publishes to.
     * The topics will be topicPrefix.<topic_name>
     * It will be used to deduce the name of the topic of the subscription to
     * the whole newspaper.
     */
    private String topicPrefix;

    /**
     * Getter for the variable 'topicPrefix'.
     * @return variable 'topicPrefix'
     */
    public String getTopicPrefix() {
        return topicPrefix;
    }
    /**
     * Setter for the variable 'topicPrefix'.
     * @param topicPrefix  new value of the variable 'topicPrefix'
     */
    public void setTopicPrefix(final String topicPrefix) {
        this.topicPrefix = topicPrefix;
    }

    /**
     * Constructor for newspapers retrieved from the database.
     */
    public Newspaper() {
        //after retrieving a newspaper from the database, its scUpdatesTopic
        //and scAssigTopic MUST be set manually
    }

    /**
     * Constructor for new newspapers.
     *
     * Opens a connection and creates a session to the broker to publish
     * messages to.
     *
     * @param name  name of the newspaper
     * @param username  username to use to connect to the broker
     * @param password  password to use to connect to the broker
     * @param topicPrefix  prefix in the topics of the newspaper
     * @param topics  list of topics to publish to
     */
    public Newspaper(final String name, final String username,
                     final String password, final String topicPrefix,
                     final Set<String> topics) {

        super(username, password, topics);

        this.name = name;
        this.topicPrefix = topicPrefix;

    }

    /**
     * Transform this newspaper in an autonewspaper (autopublishing newspaper).
     *
     * @param ts  first time segment of the autonewspaper
     * @return  this newspaper as an autonewspaper
     */
    public AutoNewspaper toAutoNewspaper(final TimeSeg ts) {
        AutoNewspaper anp = new AutoNewspaper(this, ts);

        //set name of topics to communicate with the overlay manager
        anp.setOmAssigTopic("brokerAssig." + anp.getClientId());
        anp.setOmUpdatesTopic("brokerUpdates." + anp.getClientId());

        //set listener to update messages from the overlay manager
        anp.setOmUpMsgListener(new UpdateMessageListener(anp));

        return anp;
    }

}
